<template>
	<view>
		<!-- 轮播图 -->
		<u-swiper :list="slides" name="img_url" height="320"></u-swiper>
		<!-- 菜单 -->
		<view class="u-m-t-20">
			<u-tabs ref="tabs" bar-width="100" item-width="160" :current="currentSort" @change="changeSort"
				:list="sortList" :is-scroll="false"></u-tabs>
		</view>
		<!-- 商品 -->
		<u-row gutter="16" class="u-skeleton">
			<u-col span="6" v-for="goods in goods.length!=0?goods:[{},{},{},{}]">
				<goods-card :goods="goods"></goods-card>
			</u-col>
		</u-row>
		<!-- 骨架屏 -->
		<u-skeleton :loading="loading" :animation="true"></u-skeleton>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				
				sortList: [{
						name: '默认',
					},
					{
						name: '销量',
					},
					{
						name: '推荐',
					},
					{
						name: '最新',
					}
				],
				currentSort: 0,
				slides:[],
				goods:[],
				page:1,
				loading:false

			}
		},
		async onLoad() {
			// await this.$u.post('api/auth/wx/bind').then((res)=>{
			// 	console.log(res)
			// }).catch(e=>{

			// })

			// const res=await this.$u.patch('/api/orders/2/confirm');
			// console.log(res);

			//集中管理
			// const res=this.$u.api.index();
			// console.log(res)

			//测试vuex
			// console.log(this.vuex_version)
			// this.$u.vuex('vuex_name','Tom')
			// console.log(this.vuex_name)
			this.getData()
			if(!this.$u.utils.isLogin()) return
			
		},
		methods: {
			//轮播图切换
			changeSort(index) {
				console.log(index)
				this.currentSort = index;
				this.goods=[]
				this.page=1
				this.getData()
			},
			//请求数据
			async getData(){
				this.loading=true
				const params={
					page:this.page,
					
				}
				if(this.currentSort==1){
					params.sales=1
				}
				if(this.currentSort==2){
					params.recommend=1
				}
				if(this.currentSort==3){
					params.news=1
				}
				
				const res = await this.$u.api.index(params).then(res=>{
					console.log(res)
					this.goods=[...this.goods,...res.goods.data]
					this.slides=res.slides
				})
				this.loading=false
				
			}
		},
		//触底事件
		onReachBottom() {
			console.log(11)
			this.page=this.page+1
			this.getData()
		}
	}
</script>

<style lang="scss" scoped>
	
</style>
